package org.e_gold.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

public class StringUtil {

	private static final Logger logger = Logger.getLogger(StringUtil.class);

	public static String prepareCookies(Map<String, String> cookies, Map<String, String> currentCookies) {

		String strCookies = "";
		// first time
		if (currentCookies == null) {
			if (cookies != null) {
				for (Map.Entry<String, String> entry : cookies.entrySet()) {
					strCookies += ";" + entry.getKey() + "=" + entry.getValue();
				}
			}
		} else {
			// append cookies
			if (cookies != null) {
				for (Map.Entry<String, String> entry : cookies.entrySet()) {
					currentCookies.put(entry.getKey(), entry.getValue());
				}
				for (Map.Entry<String, String> entry : currentCookies.entrySet()) {
					strCookies += ";" + entry.getKey() + "=" + entry.getValue();
				}
			}
		}
		return strCookies.length() > 0 ? strCookies.substring(1) : "";
	}

	public static Float getBalanceFromString(String totalBalanceStr) {

		Float amount = 0F;
		if (!StringUtils.isEmpty(totalBalanceStr)) {
			totalBalanceStr = totalBalanceStr.substring(0, totalBalanceStr.length() - 4);
			amount = Float.parseFloat(totalBalanceStr);
		}
		return amount;
	}

	public static String getName(String text) {

		String[] _name = text.split(" ");
		String name = "";
		for (int j = 0; j < _name.length - 1; j++) {
			name += " " + _name[j];
		}
		return name.substring(1);
	}

	public static String getContractType(String contractStr) {

		if (!StringUtils.isEmpty(contractStr)) {
			String[] contractType = contractStr.split(" ");
			String typeName = "";
			for (int i = 1; i < contractType.length; i++) {
				typeName += " " + contractType[i];
			}
			return typeName.substring(1);
		}
		return null;
	}

	public static void main(String a[]) {

		int ab = Math.round(Float.parseFloat("7.5"));
		logger.info(ab);
	}

	public static String getDate() {

		return new SimpleDateFormat("YYYY-MM-dd_hh-mm-ss").format(new Date());
	}

	// ex : singaporetech.edu.sg.hiringboss.com -> singaporetech.edu.sg
	public static String getDir(String name) {

		String dir = "";
		String[] s_ = name.split("\\.");
		for (int i = 0; i < s_.length - 2; i++) {
			dir += "." + s_[i];
		}
		if (name.indexOf("lg") > -1) {
			return "lg";
		}
		return dir.substring(1);
	}
}
